package com.tom.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

/***
 * 采集携程数据
 * @author Tom
 *
 */
public class CtripDataImpl {
	
	public static void main(String[] args){
		String url = "http://flights.ctrip.com/domesticsearch/search/SearchFirstRouteFlights?DCity1=TAO&ACity1=CTU&SearchType=S&DDate1=2018-05-18&IsNearAirportRecommond=0&LogToken=2b8e9c57e9bd46839edb35e981bf0a96&rk=6.442566969705046094512&CK=804CC37E0B4610F016C910FEEF3ECEEA&r=0.4654814139198291475116";
		System.err.println(url);
		String cookies = "Union=SID=155952&AllianceID=4897&OUID=baidu81|index|||; Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; _abtest_userid=d59cc03b-aa45-4d9d-a01b-852d49466f9d; adscityen=Qingdao; traceExt=campaign=CHNbaidu81&adid=index; MKT_Pagesource=PC; _ga=GA1.2.119872467.1526363210; _gid=GA1.2.1531316543.1526363210; _RF1=58.58.35.146; _RSG=7bahGFNvme5lxnEmlAokXB; _RDG=28357050a6246b23622812281ce9dcd0c8; _RGUID=5a30e573-d0ae-4947-b86e-c8abf03b1eef; FD_SearchHistorty={\"type\":\"S\",\"data\":\"S%24%u9752%u5C9B%28TAO%29%24TAO%242018-05-15%24%u5317%u4EAC%28BJS%29%24BJS%24%24%24\"}; _bfa=1.1526363199616.32m0ju.1.1526363199616.1526363199616.1.3; Mkt_UnionRecord=%5B%7B%22aid%22%3A%224897%22%2C%22timestamp%22%3A1526363265819%7D%5D; _jzqco=%7C%7C%7C%7C1526363211767%7C1.1790962422.1526363210460.1526363225465.1526363265864.1526363225465.1526363265864.undefined.0.0.3.3; __zpspc=9.1.1526363210.1526363265.3%231%7Cbaidu%7Ccpc%7Cbaidu81%7C%25E6%2590%25BA%25E7%25A8%258B%7C%23; _bfi=p1%3D101027%26p2%3D101027%26v1%3D3%26v2%3D2";
		String result = HttpRequest.postByCookies(url, "", cookies);
		System.out.println(result);
		JSONObject res = JSONObject.parseObject(result);
		//航班信息
		JSONArray flyArr = res.getJSONArray("fis");
		String date = "20180518";
		ArrayList<String> list = new ArrayList<String>();
		for(int i = 0 ; i < flyArr.size() ; i++){
			//出发地
			String source = flyArr.getJSONObject(i).getString("dcn");
			System.err.println(source);
			//目的地
			String des = flyArr.getJSONObject(i).getString("acn");
			System.err.println(des);
			//出发地航站楼
			String source_port = flyArr.getJSONObject(i).getString("dpbn");
			System.err.println(source_port);
			//目的地航站楼
			String des_port = flyArr.getJSONObject(i).getString("apbn");
			System.err.println(des_port);
			//出发时间
			String start = flyArr.getJSONObject(i).getString("dt");
			System.err.println(start);
			//到达时间
			String end = flyArr.getJSONObject(i).getString("at");
			System.err.println(end);
			//航空公司信息
			String air_com = flyArr.getJSONObject(i).getString("alc");
			String com_name = res.getJSONObject("als").getString(air_com);
			System.err.println(com_name);
			//航班号
			String airplane_no = flyArr.getJSONObject(i).getString("fn");
			System.err.println(airplane_no);
			//价格
			String price = flyArr.getJSONObject(i).getString("lp");
			System.err.println(price);
			//准点率
			String str = flyArr.getJSONObject(i).toJSONString();
			String hisP = str.split("HistoryPunctualityArr")[1].split("\"")[2].replace("\\", "");
			System.err.println(hisP);
			
			ReadContext context = JsonPath.parse(str);
			String test = context.read("$.lcfp").toString();
			System.err.println("---------"+test);
			//生成插入语句
			String sql = "insert into ctrip_data(fly_info,source,source_port,start_time,destination,destination_port,end_time,fly_date,history_rate,price) "
					+ "values('"+com_name+airplane_no+"','"+source+"','"+source_port+"','"+start+"','"+des+"','"+des_port+"','"+end+"','"+date+"','"+hisP+"','"+price+"')";
			list.add(sql);
		}
		/*try {
			JDBCUtil.CtripJsonTransfer(list);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

}
